package com.example.jussi.httpurlconnectionpostdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 依赖 blog (Tomcat HttpServlet)
 */
public class MainActivity extends AppCompatActivity {
    EditText nickName, content;
    TextView textView;
    String result;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0x01){
                textView.append(result);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nickName = (EditText) findViewById(R.id.editText);
        content = (EditText) findViewById(R.id.editText2);
        textView = (TextView) findViewById(R.id.textView3);
        textView.setText("");
    }

    public void send(View source){
        result = "";
        new Thread(){
            @Override
            public void run() {
                super.run();
                String pathString = "http://192.168.1.106:8080/blog/dealPost.jsp";
                try {
                    URL url = new URL(pathString);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(true);
                    connection.setInstanceFollowRedirects(true);
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    OutputStream outputStream = connection.getOutputStream();
                    PrintStream printStream = new PrintStream(outputStream);
                    String string = "nickname=" + nickName.getText().toString() + "&content=" + content.getText().toString();
                    printStream.println(string);
                    if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                        InputStream inputStream = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                        String string2;
                        while((string2 = reader.readLine()) != null){
                            result += string2 + "\n";
                        }
                        handler.sendEmptyMessage(0x01);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
